function gewMaterial(effect,name)
{
  this.effect = effect;
  this.gl = effect.gl;
  this.batches = [];
  this.nodeList = [];
  if(name)
    this.name = name;
  else
  {
    this.name = "Material"+getUniqueNumber();
  }
  effect.materialList[this.name] = this;
  // private
  this.isRendering = false;
  this.batchType = GEW_NO_BATCH;
}

gewMaterial.prototype = {
  destructor:function()
  {
    var list  = this.nodeList;
    var listLen = list.length;  
    for (var i = 0; i < listLen; i++) {
      list[0].removeMaterial(this);
    }
    var e = this.effect;
    delete e.materialList[this.name];
  },
  addNode: function(node)
  {
    this.nodeList.push(node);
  },
  removeNode: function(node)
  {
    var batchList = this.batches;
    var batchListLen = batchList.length;
    for (var i = 0; i < batchListLen; i++) 
    {
      batchList[i].removeNode(node);
    }
    var list = this.nodeList;
    var listLen = list.length;
    for (var i = 0; i < listLen; i++) 
    {
      if (list[i] == node) 
      {
        list.splice(i, 1);
        break;
      }
    }
  },
  updateBatches: function()
  {
    var batchType = this.batchType;
    if (batchType == GEW_STATIC_BATCH && this.batches.length == 0) 
    {
      var nodeList = this.nodeList;
      var nodeListLen = nodeList.length;
      for (var i = 0; i < nodeListLen; i++) 
      {
        var node = nodeList[i];
        var isBatched = false;
        var batchList = this.batches;
        var listLen = batchList.length;
        for (var b = 0; b < listLen; b++) 
        {
          isBatched = batchList[b].addNode(node);
          if (isBatched) 
            break;
        }
        if (!isBatched) 
        {
          var new_batch = new gewStaticBatch();
          new_batch.addNode(node);
          batchList.push(new_batch);
        }
      }
      
      var bLen = batchList.length;
      for (var bb = 0; bb < bLen; bb++) 
      {
        batchList[bb].commit(this.gl);
      }
    }
  }
}
